import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

public class Klient1 extends JPanel 
    implements Runnable, ActionListener{
   private static final long serialVersionUID = 1L;
   JTextArea ta=new JTextArea();
   JTextField tf=new JTextField();
   PrintWriter vorku;
   BufferedReader vorgust;
   public Klient1(){
	  setLayout(new BorderLayout());
	  add(new JScrollPane(ta), BorderLayout.CENTER);
	  add(tf, BorderLayout.SOUTH);
	  tf.addActionListener(this);
	  try{
		 Socket sc=new Socket("greeny.cs.tlu.ee", 3000);
		 vorku=new PrintWriter(sc.getOutputStream(), true);
		 vorgust=new BufferedReader(
				 new InputStreamReader(sc.getInputStream()));
		 new Thread(this).start();
	  } catch(Exception ex){ex.printStackTrace();}
   }
   public void actionPerformed(ActionEvent e){
	  if(e.getSource()==tf){
		  vorku.println(tf.getText());
		  tf.setText("");
	  }
   }
   public void run(){	   
	  while(true){	
		try{
		 String rida=vorgust.readLine();
		 ta.append(rida+"\n");
		}catch(Exception ex){ex.printStackTrace();}
	  }
   }
   public static void main(String[] arg){ 	   
	   JFrame f=new JFrame("Jututoa klient");
	   f.getContentPane().add(new Klient1());
	   f.setSize(400, 300);
	   f.setVisible(true);
	   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}